//
//  JAListViewController.m
//  网易新闻
//
//  Created by Arvin on 17/3/18.
//  Copyright © 2017年 Arvin. All rights reserved.
//

#import "JAListViewController.h"
#import "CZAdditions.h"
#import "UILabel+CZAddition.h"
#import <Masonry.h>
#import "JANetworkTool.h"
#import "JANetworkTool+News.h"
#import "JANewsNormalCell.h"
#import "JANewsHeadCell.h"
#import "JANewsMutiImageCell.h"
#import "JANewsBigImageCell.h"
#import "JADetailViewController.h"

static NSString *identifier = @"newListCell";
static NSString *headIdentifier = @"headNewsListCell";
static NSString *bigIdentifier = @"WYBigImageCell";
static NSString *mutiIdentifier = @"mutiImageCell";

@interface JAListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataSourceArr;

@end

@implementation JAListViewController

-(id)initWithIndex:(NSInteger)index channelModel:(JAChannelModel *)model{
    
        if(self = [super init])
        {
            _index = index;
            _model = model;
            
            //初始化
            _dataSourceArr = [NSMutableArray array];
        }
    
        return self;
    
}

-(void)viewDidLoad{
    [super viewDidLoad];

    [self setupUI];
    
    [self loadNewsData];
}

-(void)setupUI{
    
    _tableView = [[UITableView alloc]init];
    [self.view addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(self.mas_bottomLayoutGuide);//底部约束会以tabbar的底部为参照
    }];
    
    //设置代理和数据源
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    //设置自动行高
    _tableView.estimatedRowHeight = 200;
    _tableView.rowHeight = UITableViewAutomaticDimension;
    
    //注册cell
    [_tableView registerClass:[JANewsNormalCell class] forCellReuseIdentifier:identifier];
     [_tableView registerClass:[JANewsHeadCell class] forCellReuseIdentifier:headIdentifier];
    [_tableView registerClass:[JANewsBigImageCell class] forCellReuseIdentifier:bigIdentifier];
    [_tableView registerClass:[JANewsMutiImageCell class] forCellReuseIdentifier:mutiIdentifier];
}
#pragma mark - 获取数据
-(void)loadNewsData{
    [[JANetworkTool sharedTool] requestNewList:_model.tid callBack:^(NSArray *modelArr) {
        [_dataSourceArr addObjectsFromArray:modelArr];
        [_tableView reloadData];
    }];
}
#pragma mark - tableView的数据源的方法
//返回行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSourceArr.count;
}
//返回每一行的cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    //设置数据
    JANewsModel *model = _dataSourceArr[indexPath.row];
    
    //如果是头条数据
    if(model.hasHead){
        JANewsHeadCell *headCell = [tableView dequeueReusableCellWithIdentifier:headIdentifier forIndexPath:indexPath];
        headCell.model = model;
        
        return headCell;
    }
    
     //是否是大图
    if(model.imgType){
        JANewsBigImageCell *bigImageCell = [tableView dequeueReusableCellWithIdentifier:bigIdentifier forIndexPath:indexPath];
        
        bigImageCell.model = model;
        
        return bigImageCell;
    }
    
    //是否是多图
    if (model.imgextra != nil) {
        JANewsMutiImageCell *mutiImageCell = [tableView dequeueReusableCellWithIdentifier:mutiIdentifier forIndexPath:indexPath];
        mutiImageCell.model = model;
        
        return mutiImageCell;
    }
    
        //正常的cell
        JANewsNormalCell *normalCell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
        normalCell.model = model;
        
        return normalCell;
    
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //获取到点击的新闻的model
    JANewsModel *model = _dataSourceArr[indexPath.row];
    
    //创建一个新闻的详细页
    JADetailViewController *detail = [[JADetailViewController alloc]init];
    
    //将model传给详细页
    detail.model = model;
    
    //推到详细页之后,隐藏底部的tabbar
    detail.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:detail animated:YES];
}

@end
